package cn.sfturing.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.sfturing.entity.*;
import cn.sfturing.service.OfficeService;
import com.alibaba.fastjson.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import cn.sfturing.service.DoctorService;
import cn.sfturing.service.HospitalService;
import cn.sfturing.utils.PageUtils;

import javax.servlet.http.HttpServletRequest;

/**
 * 
 * @author sfturing
 *
 * @date 2017年5月31日
 */
@Controller
public class DoctorController {
	@Autowired
	private DoctorService doctorService;
	@Autowired
	private HospitalService hospitalService;
	@Autowired
	private PageUtils pageUtils;
	@Autowired
	private OfficeService officeService;

	/**
	 * 医生主界面(推荐医生)
	 * 
	 * @return
	 */
	@RequestMapping(value = "/doctorIndex/{page}")
	public String officeIdex(Model model, @PathVariable("page") int page) {
		// 查询推荐的医院
		List<Hospital> hospitalRe = hospitalService.findHosByRe();
		// 设置页面
		pageUtils.setCurrentPage(page);
		pageUtils.setTotalRecord(doctorService.findDoctorByReNum(hospitalRe));
		int start;
		if (pageUtils.getCurrentPage() == 0) {
			start = 0;
		} else {
			start = pageUtils.getPageRecord() * (pageUtils.getCurrentPage() - 1);
		}
		Map<String, Object> doctorMap = new HashMap<String, Object>();
		doctorMap.put("list", hospitalRe);
		doctorMap.put("start", start);
		doctorMap.put("size", pageUtils.getPageRecord());
		List<Doctor> doctorRe = doctorService.findDoctorByRe(doctorMap);
		model.addAttribute("pages", pageUtils);
		model.addAttribute("doctorRe", doctorRe);
		return "doctor/doctorIndex";
	}

	/**
	 * 医生详情
	 * 
	 * @return
	 */
	@RequestMapping(value = "/doctorInfoShow/{id}", method = RequestMethod.GET)
	public String hosInfoShow(Model model, @PathVariable(value = "id") int id) {
		Doctor doctor = doctorService.findDoctorById(id);
		Hospital hospital = hospitalService.findHosByName(doctor.getHospitalName());
		model.addAttribute("hos", hospital);
		model.addAttribute("doctor", doctor);
		return "doctor/doctorInfoShow";
	}

	/**
	 * 全部医生
	 * 
	 * @return
	 */
	@RequestMapping(value = "/allDoctor/{page}")
	public String orderOffcie(Model model, @PathVariable("page") int page, Doctor doctor) {
		// 将输入条件传回前台
		CommonCondition commonCondition = new CommonCondition();
		commonCondition.setHospitalName(doctor.getHospitalName());
		commonCondition.setOfficesName(doctor.getOfficesName());
		commonCondition.setDoctorName(doctor.getDoctorName());
		commonCondition.setDoctorTitle(doctor.getDoctorTitle());
		commonCondition.setDoctorDegree(doctor.getDoctorDegree());
		commonCondition.setDoctorAdministrative(doctor.getDoctorAdministrative());
		pageUtils.setCurrentPage(page);
		pageUtils.setTotalRecord(doctorService.findDoctorNum(doctor));
		int start;
		if (pageUtils.getCurrentPage() == 0) {
			start = 0;
		} else {
			start = pageUtils.getPageRecord() * (pageUtils.getCurrentPage() - 1);
		}
		List<Doctor> doctorRe = doctorService.findDoctorByCondition(doctor, start, pageUtils.getPageRecord());
		// 查询医生的职位
		List<String> doctorTitle = doctorService.findDoctorTitle();
		List<String> doctorAdministrative = doctorService.findDoctorAdministrative();
		List<String> doctorDegree = doctorService.findDoctorDegree();
		model.addAttribute("pages", pageUtils);
		model.addAttribute("doctorRe", doctorRe);
		// 查询条件
		model.addAttribute("commonCondition", commonCondition);
		// 将查询的医生职称传到前台
		model.addAttribute("doctorTitle", doctorTitle);
		model.addAttribute("doctorAdministrative", doctorAdministrative);
		model.addAttribute("doctorDegree", doctorDegree);
		return "doctor/doctor";
	}
	//查询列表
	@RequestMapping(value="doctor/list.html")
	public String getUserList(Model model, Doctor qDoctor,HttpServletRequest request){
		Admin admin=(Admin) request.getSession().getAttribute("adminSession");
		List<Doctor> houseList=new ArrayList<>();
		if(admin.getRank()>1){
			houseList = doctorService.queryBk(qDoctor);
		}else {
			qDoctor.setHospitalName(admin.getHospitalName());
			houseList=doctorService.everythingQuery(qDoctor);
		}
		model.addAttribute("qDoctor",qDoctor);
		model.addAttribute("doctorList", houseList);
		return "bk/doctorlist";
	}

	//删除医生
    @RequestMapping(value = "/delDoctor.json", method = RequestMethod.GET)
    @ResponseBody
    public Object delHouse(@RequestParam String id){
        HashMap<String, String> resultMap = new HashMap<String, String>();
        if(id == null || id == ""){
            resultMap.put("delResult", "notexist");
        }else{
            if(doctorService.byIdDel(Integer.parseInt(id))){
				resultMap.put("delResult", "true");
            }else{
                resultMap.put("delResult", "false");
            }
        }
        return JSONArray.toJSONString(resultMap);
    }

	/**
	 * 添加医生
	 */
	@RequestMapping(value = "/doctor/toAdd.html", method = RequestMethod.GET)
	public String toAdd() {
		return "bk/doctoradd";
	}

	/**
	 * 保存医生信息
	 */
	@RequestMapping(value = "/doctor/addSave.json", method = RequestMethod.POST)
	@ResponseBody
	public Object upDataSave(@RequestBody Doctor doctor){
		//数据处理
		HashMap<String, String> resultMap = new HashMap<String, String>();
		if(doctorService.allDoctor(doctor)){
			resultMap.put("addResult", "true");
		}else{
			resultMap.put("addResult", "false");
		}
		return JSONArray.toJSONString(resultMap);
	}

	@RequestMapping(value = "/doctor/toModify.html", method = RequestMethod.GET)
	public String toModify(@RequestParam("id") Integer id, Model model) {
		Doctor doctor = doctorService.findDoctorById(id);
		model.addAttribute("doctor", doctor);
		return "bk/doctormodify";
	}

	/**
	 * 修改医生信息
	 */
	@RequestMapping(value = "/doctor/modifySave.json", method = RequestMethod.POST)
	@ResponseBody
	public Object upDataSave2(@RequestBody Doctor doctor){
		//数据处理
		HashMap<String, String> resultMap = new HashMap<String, String>();
		if(doctorService.updataAll(doctor)){
			resultMap.put("addResult", "true");
		}else{
			resultMap.put("addResult", "false");
		}
		return JSONArray.toJSONString(resultMap);
	}

	/**
	 * 修改医生信息
	 */
	@RequestMapping(value = "/doctor/updateSave.json", method = RequestMethod.POST)
	@ResponseBody
	public Object UpdateSave(@RequestBody Doctor doctor){
		//数据处理
		HashMap<String, String> resultMap = new HashMap<String, String>();
		if(doctorService.updataAll(doctor)){
			resultMap.put("addResult", "true");
		}else{
			resultMap.put("addResult", "false");
		}
		return JSONArray.toJSONString(resultMap);
	}

	@RequestMapping(value = "/doctor/toModifyPwd")
	public String toModifyPwd(){
		return "bk/doctorUpdataPassword";
	}
	//修改密码
	@RequestMapping(value = "/doctor/updatePassword", method = RequestMethod.POST)
	public String UpdataPassword(@RequestParam("password") String password, @RequestParam("newpassword") String newpassword, HttpServletRequest request, Model model){
		Doctor doctor= (Doctor) request.getSession().getAttribute("doctorSession");
		System.out.println("========================="+password+"================"+newpassword+"==============="+doctor.getPassword());
		if(!doctor.getPassword().equals(password)){
			model.addAttribute("updatePasswordResult","false");
			return "bk/adminUpdataPassword";
		}
		doctor.setPasswrod(newpassword);
		if(doctorService.updataAll(doctor)){
			model.addAttribute("updatePasswordResult","true");
		}else{
			model.addAttribute("updatePasswordResult","false");
		}
		return "bk/doctorUpdataPassword";
	}

}
